﻿using ConsoleAppEFCoreTest1.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppEFCoreTest1
{
    public class LocalContext : DbContext
    {
        public DbSet<Book>   Books { get; set; }
        public DbSet<Person> Persons { get; set; }

        public DbSet<Student> Students { get; set; }

        public DbSet<Teacher> Teachers { get; set; }

        public readonly LoggerFactory loggerFactory = new LoggerFactory(new[] { new DebugLoggerProvider() });

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //获取当前程序集的 config
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            string linkDbStr = "server=127.0.0.1,5200;database=demo1;uid=sa;pwd=Sql123456!";
            optionsBuilder.UseSqlServer(linkDbStr);
            //输出到调试窗口
            //optionsBuilder.UseLoggerFactory(loggerFactory);
            //简单日志输出
            optionsBuilder.LogTo(Console.WriteLine);
        }
    }
}
